package ro.cityprojects.adminloc.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import ro.cityprojects.adminloc.dao.BasicRepository;
import ro.cityprojects.adminloc.entities.Entrance;
import ro.cityprojects.adminloc.exceptions.EntityNotFoundException;
import ro.cityprojects.adminloc.exceptions.ValidationException;
import ro.cityprojects.adminloc.metadata.EntityMetadata;

import java.util.Set;

/**
 * EntranceServiceImpl
 *
 * @author bogdan.solga
 *
 * Date: 17/10/13, time: 12:26
 */
@Service
public class EntranceServiceImpl extends BaseService implements EntranceService {

    @Autowired
    private BasicRepository<Integer, Entrance> repository;

    @Override
    public String[] getDefaultFields() {
        return new String[]{"id", "name", "apartmentsNumber"};
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(Entrance entrance) throws ValidationException {
        repository.save(entrance);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String get(Integer primaryKey, String includedFields) throws EntityNotFoundException {
        Entrance entrance = repository.get(Entrance.class, primaryKey);

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(entrance, outputFields);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String getAll(String filterName, String filterValue, String includedFields) {
        Set<Entrance> entrances = repository.getAll(Entrance.class, filterName, filterValue);

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(entrances, outputFields);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void update(Entrance entrance, Integer primaryKey) throws ValidationException {
        Entrance updated = repository.get(Entrance.class, primaryKey);

        BeanUtils.copyProperties(entrance, updated);

        repository.update(updated);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delete(Integer primaryKey) throws EntityNotFoundException {
        repository.delete(Entrance.class, primaryKey);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public void checkExisting(Entrance entrance) throws ValidationException {
        String entranceName = entrance.getName();
        Entrance existing = repository.getByProperty(Entrance.class, "name", entranceName);
        if (existing != null) {
            throw new ValidationException("Exista deja o scara cu numele '" + entranceName + "'");
        }
    }

    @Override
    public String getMetadata() {
        return getMetadata(EntityMetadata.Entrance);
    }
}
